#include "KHtmlParser.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char g_szElemName[][9] = {
	" ", 
	"html",};

int khtml_FindFirstTagBeginSymbol(khtml_buffer *buf)
{
	char *pbuffer = (char*)buf->m_pBuffer;
	int i = buf->m_iCurProcPos;
	int ilen = buf->m_iBufSize;
	for (; i < ilen; i++)
	{
		if (pbuffer[i] == '<')
		{
			return i;
		}
	}
	return -1;
}

int khtml_FindFirstTagEndSymbol(khtml_buffer *buf)
{
	char *pbuffer = (char*)buf->m_pBuffer;
	int i = buf->m_iCurProcPos;
	int ilen = buf->m_iBufSize;
	for (; i < ilen; i++)
	{
		if (pbuffer[i] == '>')
		{
			return i;
		}
	}
	return -1;
}

int khtml_FindSecondTagBeginSymbol(khtml_buffer *buf)
{
	char *pbuffer = (char*)buf->m_pBuffer;
	int i = buf->m_iCurProcPos;
	int ilen = buf->m_iBufSize;
	for (; i < ilen; i++)
	{
		if (pbuffer[i] == '<' && pbuffer[i+1] == '/')
		{
			return i;
		}
	}
	return -1;
}

int khtml_FindSecondTagEndSymbol(khtml_buffer *buf)
{
	char *pbuffer = (char*)buf->m_pBuffer;
	int i = buf->m_iCurProcPos;
	int ilen = buf->m_iBufSize;
	for (; i < ilen; i++)
	{
		if (pbuffer[i] == '>')
		{
			return i;
		}
	}
	return -1;
}

bool khtml_FindItemNameInTag(khtml_buffer *buf, int end, int *begin, int *len)
{
	char *pbuffer = (char*)buf->m_pBuffer;
	int i = buf->m_iCurProcPos;
	for (; i < end; i++)
	{
		if (pbuffer[i] != ' ' && 
			pbuffer[i] != '/' &&
			pbuffer[i] != '=' &&
			pbuffer[i] != '\"')
		{
			*begin = i;
			break;
		}
	}
	if (i == end)
	{
		return false;
	}

	for (; i < end; i++)
	{
		if (pbuffer[i] == ' ' || 
			pbuffer[i] == '/' ||
			pbuffer[i] == '=' || 
			pbuffer[i] == '"')
		{
			*len = i - (*begin);
			break;
		}
	}
	return true;
}

bool khtml_FindItemValueInTag(khtml_buffer *buf, int end, int *begin, int *len)
{
	int i1, i2;
	char *pbuffer = (char*)buf->m_pBuffer;
	int i = buf->m_iCurProcPos;
	for (; i < end; i++)
	{
		if (pbuffer[i] == '"')
		{
			i1 = i;
			break;
		}
	}
	if (i == end)
	{
		return false;
	}

	for (; i < end; i++)
	{
		if (pbuffer[i] == '"')
		{
			i2 = i;
			break;
		}
	}
	*begin = i1;
	*len = i2 - i1;
	return true;
}
khtml_content* khtml_parsebuffer(khtml_buffer *buf, khtml_content_type eType)
{
	int pos1, pos2;
	int begin, len;
	char *pbuffer = (char*)buf->m_pBuffer;
	khtml_content* pContent;
	khtml_elemattr *pElemAttr = NULL;
	switch(eType)
	{
	case khc_html:
		{
			while(1)
			{
				pos1 = khtml_FindFirstTagBeginSymbol(buf);
				if (pos1 == -1)
				{
					return NULL;
				}
				pos2 = khtml_FindFirstTagEndSymbol(buf);
				if (pos2 == -1)
				{
					return NULL;
				}

				buf->m_iCurProcPos = pos1;
				if (khtml_FindItemNameInTag(buf, pos2, &begin, &len))
				{
					if (len == 4 && memcmp(g_szElemName[eType], pbuffer+begin, 4) == 0)
					{
						pContent = (khtml_content*)malloc(sizeof(khtml_content));
						if (!pContent)
						{
							return NULL;
						}

						pContent->m_Type = eType;
						pContent->m_pParent = NULL;
						pContent->m_pBrother = NULL;
						pContent->m_pElemAttr = NULL;
						
						buf->m_iCurProcPos = begin + len;

						if (khtml_FindItemNameInTag(buf, pos2, &begin, &len))
						{
							
						}

						break;
					}
				}
				buf->m_iCurProcPos = pos2 + 1;
			}

			break;
		}
	}
	return NULL;
}